//
//  CLTopicViewController.m
//  chanlin
//
//  Created by Ryan on 14-12-15.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLTopicViewController.h"
#import "CLTopic.h"
#import "IFCoreDataManager.h"
#import "IFIndexCell.h"
#import "IFIndexFocusCell.h"
#import "IFIndexIconCell.h"
#import "IFIndexSlidesCell.h"
#import "CLTopicImgCell.h"
#import "CLTopicModel.h"
#import "CLTopicTextCell.h"
#import "CLPushAnimatedRight.h"
#import "CLSlidersViewController.h"
#import "CLDocViewController.h"
#import "CLVideoViewController.h"
#import "ReadDocHistory.h"
#import "CLLiveViewController.h"
#import "CLWebVeiwController.h"


@interface CLTopicViewController ()

@property (nonatomic,strong) NSString *shareUrl;

@end

@implementation CLTopicViewController

- (void)dealloc{
    NSLog(@"loadtopic");
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.tableView hiddenBottom:YES];
   
    self.tableView.frame=CGRectMake(0, 20.f, GlobleWidth, GlobleHeight-20.f);
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"sort" ascending:YES];
    NSFetchRequest *req = [[NSFetchRequest alloc] initWithEntityName:@"CLTopic"];
    req.predicate = [NSPredicate predicateWithFormat:@"topicId=%@", self.itemId];
    req.sortDescriptors = @[sort];
    self.fetch = [[NSFetchedResultsController alloc] initWithFetchRequest:req
                                                     managedObjectContext:[IFCoreDataManager sharedInstance].mainMoc
                                                       sectionNameKeyPath:@"area"
                                                                cacheName:nil];
    self.fetch.delegate=self;
    [self.fetch performFetch:nil];
    
    if([self isViewLoaded])
        [self reloadTable];
    
    [self.tableView becameHeadReflashLoading];
    
    [MobClick event:@"page"
         attributes:@{@"pageId":[@"topic_" stringByAppendingString:_safeStr(self.itemId)],
                      @"title":_safeStr(self.title)}];

    
//    [[CLTopicModel sharedInstance] getListWithTopicId:self.itemId uiBlock:^(BOOL success, id responseObj, NSString *message) {
//        
//    }];
    
    self.tool=[self showToolbarWithType:IFActive_ButtomToolBarButtonTypeCollect|IFActive_ButtomToolBarButtonTypeShare];
    __weak typeof(self) me=self;
    [self.tool handleTap:IFActive_ButtomToolBarButtonTypeShare block:^(id sender) {
        if(_isStrNULL(me.shareUrl)){
            [[CLGloble sharedInstance] showTieSuccessMessage:@"现在不能分享" subMessage:@"缺少分享地址"];
            return ;
        }
        
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic sgrSetObject:[UIImage imageNamed:@"icon114.png"] forKey:@"icon"];
        
        [dic sgrSetObject:me.title?me.title:@"禅林" forKey:@"title"];
        [dic sgrSetObject:me.shareUrl forKey:@"url"];
        [me openShare:dic];
    }];
    self.tool.collectId=self.itemId;

}

-(void)jTableViewStartHeadLoading:(JTableView *)tableView{
  
    __weak typeof(self) me=self;
    [[CLTopicModel sharedInstance] getListWithTopicId:self.itemId uiBlock:^(BOOL success, id responseObj, NSString *message) {
        if(success){
            me.shareUrl=[responseObj sgrGetStringForKey:@"shareurl"];
            [me.tableView stopHeadLoading];
        }else{
            [[CLGloble sharedInstance] showTieAlertMessage:@"加载失败"
                                                subMessage:message];
        }
    }];
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    id <NSFetchedResultsSectionInfo> sectionInfo=[[self.fetch sections] objectAtIndex:section];
    NSString *name=[sectionInfo name];
    if([@"0" isEqualToString:name]) return 0;
    return 44.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    id <NSFetchedResultsSectionInfo> sectionInfo=[[self.fetch sections] objectAtIndex:section];
    NSString *name=[sectionInfo name];
    if([@"0" isEqualToString:name]) return nil;
    UIView *head=[[UIView alloc] initWithFrame:CGRectMake(0, 0, GlobleWidth, 44.f)];
    head.backgroundColor=[UIColor whiteColor];
    UILabel *title=[[UILabel alloc] initWithFrame:CGRectMake(15, 0, GlobleWidth-30, 44.f)];
    title.font=[UIFont boldSystemFontOfSize:16];
    title.textColor=[UIColor blackColor];
    title.text=[NSString stringWithFormat:@"| %@",name];
    [head addSubview:title];
    
    return head;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CLTopic *newsList=[self.fetch objectAtIndexPath:indexPath];
    return newsList.cHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CLTopic *list=[self.fetch objectAtIndexPath:indexPath];
    IFIndexCell *cell=[self cellForStyle:list.style tableView:tableView path:(NSIndexPath *)indexPath];
    [cell loadTopicData:list];
    [cell setPlayVideo:list.link==CLList_video];
    if(list.link!=CLList_sub)
        [cell setIsRead:[[ReadDocHistory shareInstance] check:list.itemId]];
    
    cell.indexPath=indexPath;
    cell.delegate=self;

    return cell;
}


- (IFIndexCell *)cellForStyle:(int)style tableView:(UITableView *)tableView path:(NSIndexPath *)indexPath{
    if(style==CLList_style_focus){
        NSString *cellID=[NSString stringWithFormat:@"IFIndexFocusCell%@%@",@(indexPath.section),@(indexPath.row)];
        // static NSString *cellID=str;
        IFIndexFocusCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[IFIndexFocusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            __weak typeof(self) me=self;
            cell._blkTapResponse2=^(CLTopic *indexList,int index,NSIndexPath *indexPath){
                CLTopic *list=[indexList.focus sgrGetType:[CLTopic class] forIndex:index];
                [me clickOnList:list];
            };
        }
        return cell;
    }else if (style==CLList_style_doc_img){
        static NSString *cellID=@"IFIndexIconCell";
        IFIndexIconCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[IFIndexIconCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }else if(style==CLList_style_doc){
        static NSString *cellID=@"IFIndexTitleCell";
        IFIndexTitleCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[IFIndexTitleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }else if(style==CLList_style_slide){
        static NSString *cellID=@"IFIndexSlidesCell";
        IFIndexSlidesCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[IFIndexSlidesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }else if(style==CLList_style_topic_img){
         static NSString *cellID=@"CLTopicImgCell";
        CLTopicImgCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[CLTopicImgCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }else if(style==CLList_style_topic_text){
        static NSString *cellID=@"CLTopicTextCell";
        CLTopicTextCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[CLTopicTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }
    
    return [[IFIndexCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"IFIndexCell"];
}

- (BOOL)showPopAction{
    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CLTopic *list=[self.fetch objectAtIndexPath:indexPath];
    [self clickOnList:list];
    IFIndexCell *cell=(IFIndexCell *)[tableView cellForRowAtIndexPath:indexPath];
    if(list.link!=CLList_sub)
        [cell setIsRead:YES];
}

- (void)didSelect:(NSIndexPath *)indexPath{
    CLTopic *list=[self.fetch objectAtIndexPath:indexPath];
    [self clickOnList:list];
    IFIndexCell *cell=(IFIndexCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    if(list.link!=CLList_sub)
        [cell setIsRead:YES];
}

- (void)clickOnList:(CLTopic *)list{
    if(list.link==CLList_sliders){
        CLSlidersViewController *slider=[CLSlidersViewController new];
        slider.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:slider];
    }else if(list.link==CLList_doc){
        CLDocViewController *doc=[CLDocViewController new];
        doc.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:doc];
    }else if(list.link==CLList_topic){
        CLTopicViewController *topic=[CLTopicViewController new];
        topic.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:topic];
    }else if(list.link==CLList_video){
        CLVideoViewController *video=[CLVideoViewController new];
        video.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:video];
    }else if(list.link==CLList_audio){
        CLDocViewController *doc=[CLDocViewController new];
        doc.itemId=list.itemId;
        doc.isAudio=YES;
        [[CLPushAnimatedRight sharedInstance] pushController:doc];
    }else if(list.link==CLList_live){
        CLLiveViewController *live=[CLLiveViewController new];
        
        live.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:live];
        return;
        
        
    }else if(list.link==CLList_web){
        CLWebVeiwController *web = [CLWebVeiwController new];
        
        web.itemId=list.url;
        [[CLPushAnimatedRight sharedInstance] pushController:web];
        return;
        
    }
    
}


@end
